﻿using System;
using System.IO;
using System.Net;

namespace PTiRouZ.Net.SilverLight.Api
{
    public class LoadFileRequest
    {
        public event EventHandler<LoadFileRequestCompletedEventArgs> LoadFileRequestCompleted;
        public event EventHandler<DownloadProgressChangedEventArgs> DownloadProgressChanged;

        private readonly string _urlFile;
        public readonly int _indexFile;
        public readonly string fileName;
       
        public LoadFileRequest(string urlFile, int indexFile)
        {
            if (urlFile == null)
                throw new ArgumentNullException("urlFile");

            _urlFile = urlFile;
            _indexFile = indexFile;
            fileName = Path.GetFileName(urlFile);
        }

        /// <summary>
        /// Execute la requête
        /// </summary>
        public void Execute()
        {
            WebClient fileLoader = new WebClient();
            fileLoader.OpenReadCompleted += FileLoaderOpenReadCompleted;
            fileLoader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(FileLoaderDownloadProgressChanged);
            fileLoader.OpenReadAsync(new Uri(_urlFile, UriKind.Absolute));            
        }

        void FileLoaderDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            EventHandler<DownloadProgressChangedEventArgs> handler = DownloadProgressChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        void FileLoaderOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                OnLoadFileRequestCompleted(new LoadFileRequestCompletedEventArgs(e.Result, _indexFile, fileName, e.Error, e.Cancelled, e.UserState));
            }
            catch (Exception ex)
            {
                OnLoadFileRequestCompleted(new LoadFileRequestCompletedEventArgs(null, _indexFile, fileName, ex, e.Cancelled, e.UserState));
            }
        }



        /// <summary>
        /// Raise the LoadFileRequestCompleted event
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLoadFileRequestCompleted(LoadFileRequestCompletedEventArgs e)
        {
            EventHandler<LoadFileRequestCompletedEventArgs> handler = LoadFileRequestCompleted;

            if (handler != null)
            {
                handler(this, e);
            }
        }
    }

    public class LoadFileRequestCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
    {
        /// <summary>
        /// Le résultat de la requête
        /// </summary>
        public Stream Result { get; private set; }

        /// <summary>
        /// L'index du fichier
        /// </summary>
        public int Index { get; private set; }

        /// <summary>
        /// Objet photo
        /// </summary>
        public string FileName { get; private set; }       

        public LoadFileRequestCompletedEventArgs(
            Stream result,
            int index,
            string filename,
            Exception e,
            bool canceled,
            object state)
            : base(e, canceled, state)
        {
            Result = result;
            Index = index;
            FileName = filename;
        }
    }
}